package net.newsmth.smthapp.android.ui;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;

import net.newsmth.smthapp.android.R;
import net.newsmth.smthapp.android.adapter.ArticleShowAdapter;
import net.newsmth.smthapp.android.helper.BBSThreadHelper;
import net.newsmth.smthapp.android.helper.BBSThreadHelperImpl;
import net.newsmth.smthapp.android.model.Article;
import net.newsmth.smthapp.android.model.BBSThread;
import net.newsmth.smthapp.android.widget.ErrorView;

import java.util.List;


public class ArticleListActivity extends BaseActivity {
    public static String BOARD_NAME_KEY = "board_name_key";
    public static String THREAD_ID_KEY = "thread_id_key";
    public static String BBSThread_KEY = "bbsthread_key";

    private BBSThread bbsThread;

    private TextView subjectTextView;
    private TextView backToBoardTextView;
    private ListView artilceListView;
    private BBSThreadHelper bbsThreadHelper;

    private int currentPage = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_list);

        bbsThreadHelper = new BBSThreadHelperImpl(this.appSettings, this.connection);

        bindControl();
        initData();
    }

    private void initData() {
        String boardName = this.getIntent().getStringExtra(BOARD_NAME_KEY);
        long threadId = this.getIntent().getLongExtra(THREAD_ID_KEY, -1);

        bbsThread = (BBSThread)this.getIntent().getSerializableExtra(BBSThread_KEY);

        subjectTextView.setText(bbsThread.getSubject());
        backToBoardTextView.setText(String.format(getResources().getString(R.string.format_back_to_board), bbsThread.getBoard()));

        // for article list view
        List<Article> articles = bbsThreadHelper.getAtricles(bbsThread, currentPage);
        ArticleShowAdapter adapter = new ArticleShowAdapter(this, articles);
        artilceListView.setAdapter(adapter);
    }

    @Override
    protected ErrorView getGeneralErrorView() {
        return null;
    }

    private void bindControl() {
        subjectTextView = (TextView)findViewById(R.id.thread_subject_text_view);
        backToBoardTextView = (TextView)findViewById(R.id.back_to_board_text_view);
        artilceListView = (ListView)findViewById(R.id.article_list_view);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.thread_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
